package com.example.bailin.giftsay.main.adapter;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.SearchGiftEntity;
import com.example.bailin.giftsay.main.SearchListener;
import com.example.bailin.giftsay.tool.MyApp;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by bailin on 16/8/23.
 *
 * 搜索页面 大家都在搜下面的适配器
 */
public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.SearchViewHolder> {
    private SearchGiftEntity entity;
    private SearchListener searchListener;

    public void setSearchListener(SearchListener searchListener) {
        this.searchListener = searchListener;
    }

    public void setEntity(SearchGiftEntity entity) {
        this.entity = entity;
    }

    @Override
    public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_search, parent, false);
        SearchViewHolder viewHolder = new SearchViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(SearchViewHolder holder, final int position) {

        holder.textView.setText(entity.getData().getHot_words().get(position));

        holder.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchListener.onClick(entity.getData().getHot_words().get(position));
            }
        });

    }

    @Override
    public int getItemCount() {
        return entity.getData().getHot_words() == null ? 0 : entity.getData().getHot_words().size();
    }

    public class SearchViewHolder extends RecyclerView.ViewHolder {
        TextView textView;
        RelativeLayout rl;
        public SearchViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv_item_search);
            rl = (RelativeLayout) itemView.findViewById(R.id.rl_item_search);
        }
    }
}
